import { reqCartList, reqDeleteCart, reqCheckCart } from '@/api'
const state = {
    cartList: []
}
const mutations = {
    GETCARTLIST(state, cartList) {
        state.cartList = cartList
    }
}
const actions = {
    // 获取购物车列表数据
    async getCartList({ commit }) {
        let result = await reqCartList()
        if (result.code == 200) {
            commit("GETCARTLIST", result.data)
        }
    },
    // 删除购物车产品
    async deleteCartListBySkuId({ commit }, skuId) {
        let result = await reqDeleteCart(skuId)
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error("false"));
        }
    },
    // 修改购物车产品选中状态
    async updateChecked({ commit }, { skuId, isChecked }) {
        let result = await reqCheckCart(skuId, isChecked)
        if (result.code == 200) {
            return 'ok';
        } else {
            return Promise.reject(new Error("false"));
        }
    }
}
const getters = {
    cartList(state) {
        return state.cartList[0] || {}
    },
    // 计算出来购物出数据
    // cartInfoList(state){
    // return state.cartList
    // }

}

export default {
    state,
    mutations,
    actions,
    getters
}